﻿#include "mainwindow.h"
#include "QtAv/hello.h"
#include <QScreen>

MainWindow::MainWindow(QWidget *parent)
    : QWidget(parent)
{
    MyDll *dll = new MyDll();
    dll->printHello();

    // 设置主窗口标题
    MainWindow::setWindowTitle("播放器");

    setWindowIcon(QIcon(":/Image/icon.jpg"));

    setupUi();
}

MainWindow::~MainWindow()
{

}

void MainWindow::resizeEvent(QResizeEvent *event)
{
    static bool Flag = true;
    if(Flag)
    {
        QScreen *screen = QGuiApplication::primaryScreen();
        QRect screenGeometry = screen->geometry();

        int screenWidth = screenGeometry.width();
        int screenHeight = screenGeometry.height();

        int windowWidth = event->size().width();
        int windowHeight = event->size().height();

        this->setGeometry(screenWidth / 2 - windowWidth / 2, screenHeight / 2 - windowHeight / 2, windowWidth, windowHeight);
        Flag = false;
    }

    // 调用父类的resizeEvent以确保父类的resizeEvent也被调用
    QWidget::resizeEvent(event);
}

void MainWindow::setupUi()
{
    m_SoftwareInfoDlg = new SoftwareInfoDialog();

    //设置主布局为垂直布局
    m_MainLayout = new QVBoxLayout();
    m_MainLayout->setSpacing(0);
    m_MainLayout->setContentsMargins(QMargins());
    setLayout(m_MainLayout);

    //控制界面，高度设置为30
    m_Control = new QWidget(this);
    m_Control->setMaximumHeight(30);

    //设置时间滑动条
    m_TimeSlider = new Slider(m_Control);
    m_TimeSlider->setDisabled(true);
    m_TimeSlider->setTracking(true);
    m_TimeSlider->setOrientation(Qt::Horizontal);
    m_TimeSlider->setMinimum(0);

    //当前时间
    m_Current = new QLabel(m_Control);
    m_Current->setToolTip(tr("Current time"));
    m_Current->setContentsMargins(QMargins(2, 2, 2, 2));
    m_Current->setText(QString::fromLatin1("00:00:00"));

    //结束时间
    m_End = new QLabel(m_Control);
    m_End->setToolTip(tr("Duration"));
    m_End->setContentsMargins(QMargins(2, 2, 2, 2));
    m_End->setText(QString::fromLatin1("00:00:00"));

    //渲染引擎
    m_Title = new QLabel(m_Control);
    m_Title->setToolTip(tr("渲染引擎"));
    m_Title->setText("Qt绘图直接渲染");
    m_Title->setIndent(8);

    //视屏播放速度
    m_Speed = new QLabel(QString::fromLatin1("1.00"));
    m_Speed->setContentsMargins(QMargins(1, 1, 1, 1));

    //暂停
    m_PlayPauseBtn = new QToolButton(m_Control);
    m_PlayPauseBtn->setIcon(QIcon(QString::fromLatin1(":/Image/play.svg")));

    //停止
    m_StopBtn = new QToolButton(m_Control);
    m_StopBtn->setIcon(QIcon(QString::fromLatin1(":/Image/stop.svg")));

    //向后
    m_BackwardBtn = new QToolButton(m_Control);
    m_BackwardBtn->setIcon(QIcon(QString::fromLatin1(":/Image/backward.svg")));

    //向前
    m_ForwardBtn = new QToolButton(m_Control);
    m_ForwardBtn->setIcon(QIcon(QString::fromLatin1(":/Image/forward.svg")));

    //打开流媒体文件
    m_OpenBtn = new QToolButton(m_Control);
    m_OpenBtn->setToolTip(tr("Open"));
    m_OpenBtn->setIcon(QIcon(QString::fromLatin1(":/Image/open.svg")));
    connect(m_OpenBtn,&QToolButton::clicked,this,&MainWindow::OpenFile);

    //信息
    m_InfoBtn = new QToolButton();
    m_InfoBtn->setToolTip(QString::fromLatin1("Media information"));
    m_InfoBtn->setIcon(QIcon(QString::fromLatin1(":/Image/info.svg")));
    connect(m_InfoBtn,&QToolButton::clicked,this,&MainWindow::SoftwareInfo);

    //音量
    m_VolumeBtn = new QToolButton();
    m_VolumeBtn->setIcon(QIcon(QString::fromLatin1(":/Image/sound.svg")));
    connect(m_VolumeBtn,&QToolButton::clicked,this,&MainWindow::ShowVolumeSlider);

    //音量滑动条
    m_VolumeSlider = new Slider();
    m_VolumeSlider->setOrientation(Qt::Horizontal);
    m_VolumeSlider->setMinimum(0);
    const int kVolumeSliderMax = 100;
    m_VolumeSlider->setMaximum(kVolumeSliderMax);
    m_VolumeSlider->setMaximumWidth(88);

    //菜单按钮
    m_MenuBtn = new QToolButton();
    m_MenuBtn->setIcon(QIcon(QString::fromLatin1(":/Image/menu.svg")));
    m_MenuBtn->setAutoRaise(true);
    m_MenuBtn->setPopupMode(QToolButton::InstantPopup);

    QMenu *subMenu = 0;
    QWidgetAction *pWA = 0;

    //打开URL界面
    m_Menu = new QMenu(m_MenuBtn);
    m_MenuBtn->setMenu(m_Menu);
    m_Menu->addAction(tr("Open Url"), this, SLOT(openUrl()));

    m_Menu->addSeparator();

    //播放列表
    subMenu = new QMenu(tr("Play list"));
    m_Menu->addMenu(subMenu);
    m_PlayList = new PlayList(this);
    m_PlayList->setSaveFile(qApp->applicationDirPath() + QString::fromLatin1("/playlist.qds"));
    m_PlayList->load();
    pWA = new QWidgetAction(0);
    pWA->setDefaultWidget(m_PlayList);
    subMenu->addAction(pWA);

    //历史列表
    subMenu = new QMenu(tr("History"));
    m_Menu->addMenu(subMenu);
    m_History = new PlayList(this);
    m_History->setMaxRows(20);
    m_History->setSaveFile(qApp->applicationDirPath() + QString::fromLatin1("/history.qds"));
    m_History->load();
    pWA = new QWidgetAction(0);
    pWA->setDefaultWidget(m_History);
    subMenu->addAction(pWA);

    m_Menu->addSeparator();
    subMenu = new QMenu(tr("Renderers"));
    m_Menu->addMenu(subMenu);

    m_RenderOptionsWidget = new OptionsWidget();
    QObject::connect(m_RenderOptionsWidget, &OptionsWidget::renderTypeChanged,this,&MainWindow::RenderTypeChanged);
    QWidgetAction *option1Action = new QWidgetAction(subMenu);
    option1Action->setDefaultWidget(m_RenderOptionsWidget);
    subMenu->addAction(option1Action);



    //主布局添加播放布局，时间滑动条，控制界面
    m_MainLayout->addWidget(m_TimeSlider);
    m_MainLayout->addWidget(m_Control);

    //设置水平布局
    QHBoxLayout *controlLayout = new QHBoxLayout();
    controlLayout->setSpacing(0);
    controlLayout->setContentsMargins(QMargins(1, 1, 1, 1));

    //主布局添加该水平布局
    m_Control->setLayout(controlLayout);
    controlLayout->addWidget(m_Current);
    controlLayout->addWidget(m_Title);
    QSpacerItem *space = new QSpacerItem(m_PlayPauseBtn->width(), m_PlayPauseBtn->height(), QSizePolicy::MinimumExpanding);
    controlLayout->addSpacerItem(space);
    controlLayout->addWidget(m_VolumeSlider);
    controlLayout->addWidget(m_VolumeBtn);
    controlLayout->addWidget(m_PlayPauseBtn);
    controlLayout->addWidget(m_StopBtn);
    controlLayout->addWidget(m_BackwardBtn);
    controlLayout->addWidget(m_ForwardBtn);
    controlLayout->addWidget(m_OpenBtn);
    controlLayout->addWidget(m_InfoBtn);
    controlLayout->addWidget(m_Speed);
    controlLayout->addWidget(m_MenuBtn);
    controlLayout->addWidget(m_End);

    //绑定时间滑动条事件
    connect(m_TimeSlider, SIGNAL(sliderMoved(int)), SLOT(seek(int)));
    connect(m_TimeSlider, SIGNAL(sliderPressed()), SLOT(seek()));
    connect(m_TimeSlider, SIGNAL(onLeave()), SLOT(onTimeSliderLeave()));
    connect(m_TimeSlider, SIGNAL(onHover(int,int)), SLOT(onTimeSliderHover(int,int)));

    initPlayer();
}

void MainWindow::seek()
{
    qDebug() << "sliderPressed";
}

void MainWindow::seek(int value)
{
    qDebug() << "sliderMoved";
    qDebug() << "value" << value;
}

void MainWindow::onTimeSliderHover(int pos, int value)
{
    qDebug() << "onHover";
    qDebug() << "pos" << pos;
    qDebug() << "value" << value;
}

void MainWindow::onTimeSliderLeave()
{
    qDebug() << "onLeave";
}

void MainWindow::initPlayer()
{
    //获取ffmpeg播放器
    m_Player = new FmpegPlayer(800,500);
    m_MainLayout->insertWidget(0, m_Player);
//    Render *render = RenderManager::GetInstance()->GetRender(RENDER_TYPE::QT_RENDER,800,500);
//    m_MainLayout->insertWidget(0, render);
    this->update();
}

void MainWindow::ShowVolumeSlider()
{
    if(m_TimeSlider->isHidden())
    {
        m_TimeSlider->setHidden(false);
    }
    else
    {
        m_TimeSlider->setHidden(true);
    }
}

void MainWindow::RenderTypeChanged(RENDER_TYPE type)
{
    if(type == QT_RENDER)
    {
        m_Title->setText("Qt绘图直接渲染");
    }
    else if(type == QT_OPENGL_RENDER)
    {
        m_Title->setText("OpenGL渲染");
    }
    else if(type == RENDER_NONE)
    {
        m_Title->setText("没有渲染类型");
    }
}

void MainWindow::SoftwareInfo()
{
    m_SoftwareInfoDlg->show();
}

void MainWindow::OpenFile()
{
    //渲染模块案列
//    QString filename = QFileDialog::getOpenFileName(nullptr, "Open YUV File", QDir::homePath(), "YUV Files (*.yuv)");
//    QFile file(filename);
//    if (!file.open(QIODevice::ReadOnly)) {
//        qDebug() << "Failed to open file";
//        return;
//    }

//    // 为YUV数据分配内存
//    int frame_size = av_image_get_buffer_size(AV_PIX_FMT_YUV420P, 1920, 1080, 1);
//    char *buffer = (char *)av_malloc(frame_size);

//    for(int i = 0;i< 200;i++)
//    {
//        file.read(buffer, frame_size);
//        memset(buffer, 0, frame_size);
//    }
//    file.read(buffer, frame_size);
//    file.close();

//    Frame *Inputframe = new Frame();
//    Inputframe->width = 1920;
//    Inputframe->height = 1080;
//    Inputframe->format = AV_PIX_FMT_YUV420P;
//    AVFrame *frame = av_frame_alloc();
//    av_image_fill_arrays(frame->data, frame->linesize, (uint8_t*)buffer, AV_PIX_FMT_YUV420P, Inputframe->width, Inputframe->height, 1);
//    Inputframe->frame = frame;

//    for(int i=0;i<m_Player->GetRenderList().size();i++)
//    {
//        m_Player->GetRenderList().at(i)->StartPlay();
//        m_Player->GetRenderList().at(i)->Draw(Inputframe);
//    }

//    av_frame_free(&Inputframe->frame);
//    Inputframe->frame = NULL;
//    delete Inputframe;

    QString filePath = QFileDialog::getOpenFileName(nullptr, "Open Media File", QDir::homePath(), "Media Files (*.mp4 *.flv *.yuv *.h264 *.h265)");
    //设置播放路径
    m_Player->SetUrl(filePath);
    //播放播放器
    m_Player->Play();

}
